package com.hujh.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;

import com.hujh.android.service.Player;

public class AudioNetActivity extends Activity {
	private Button btnPause, btnPlayUrl, btnStop;
	private SeekBar skbProgress;
	private Player player;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_audio_net);
	
		this.setTitle("在线音乐播放---hellogv编写");

		btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl);
		btnPlayUrl.setOnClickListener(new ClickEvent());

		btnPause = (Button) this.findViewById(R.id.btnPause);
		btnPause.setOnClickListener(new ClickEvent());

		btnStop = (Button) this.findViewById(R.id.btnStop);
		btnStop.setOnClickListener(new ClickEvent());

		skbProgress = (SeekBar) this.findViewById(R.id.skbProgress);
		skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent());
		player = new Player(skbProgress);

	}

	class ClickEvent implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			if (arg0 == btnPause) {
				player.pause();
			} else if (arg0 == btnPlayUrl) {
				//在百度MP3里随便搜索到的,大家可以试试别的链接
				//String url="http://219.138.125.22/myweb/mp3/CMP3/JH19.MP3";
				//String url = "http://media2.myyule.cn/group1/M00/41/E8/CqoKelE1cgOjp2C5AEKlZnRqZxo865.mp3";
				//String url = "http://media2.myyule.cn/group1/M00/41/E6/CqoKelEwEGjwx-nKAIPapCDb1QE194.mp3";
				String url = "http://zhangmenshiting.baidu.com/data2/music/35547880/35506859212400128.mp3?xcode=657741220a019dbba79c7fd72cefb648";
				player.playUrl(url);
			} else if (arg0 == btnStop) {
				player.stop();
			}
		}
	}

	class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
		int progress;

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// 原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()
			this.progress = progress * player.mediaPlayer.getDuration()
					/ seekBar.getMax();
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {

		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// seekTo()的参数是相对与影片时间的数字，而不是与seekBar.getMax()相对的数字
			player.mediaPlayer.seekTo(progress);
		}
	}

}
